package com.codedancer.campus.manager.api.user;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codedancer.campus.common.anno.InterfacePermission;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.response.ApiResponse;
import com.codedancer.campus.manager.api.user.ro.UserPageSearchRo;
import com.codedancer.campus.manager.api.user.ro.UserUpdateRo;
import com.codedancer.campus.manager.service.IUserService;
import com.codedancer.campus.manager.service.bo.user.UserPageSearchBo;
import com.codedancer.campus.manager.service.bo.user.UserUpdateBo;
import com.codedancer.campus.manager.service.vo.user.UserInfoVo;
import com.codedancer.campus.manager.service.vo.user.UserPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/user")
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "用户Api", description = "用户Api", tags = "用户Api")
public class UserApi extends BaseApi {

    @Autowired
    private IUserService userService;

    @GetMapping("/page")
    @ApiOperation("分页查询用户")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<IPage<UserPageVo>> page(
        @ApiParam(value = "用户分页查询Ro", required = false) UserPageSearchRo userPageSearchRo) {
        UserPageSearchBo userPageSearchBo = BeanUtil.copyProperties(userPageSearchRo,
            UserPageSearchBo.class);
        return success(userService.page(userPageSearchBo));
    }

    @PutMapping("/enable/{uuid}")
    @ApiOperation("启用用户")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> enable(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "用户uuid", required = true) String uuid) {
        userService.enable(uuid);
        return success();
    }

    @PutMapping("/disable/{uuid}")
    @ApiOperation("禁用用户")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> disable(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "用户uuid", required = true) String uuid) {
        userService.disable(uuid);
        return success();
    }

    @DeleteMapping("/delete/{uuid}")
    @ApiOperation("删除用户")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> delete(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "用户uuid", required = true) String uuid) {
        userService.delete(uuid);
        return success();
    }

    @PostMapping("/export")
    @ApiOperation("导出用户")
    @InterfacePermission(encodes = {"superAdmin"})
    public void export(HttpServletRequest request, HttpServletResponse response,
        @ApiParam(value = "用户分页查询Ro", required = false) UserPageSearchRo userPageSearchRo) {
        UserPageSearchBo userPageSearchBo = BeanUtil
            .copyProperties(userPageSearchRo, UserPageSearchBo.class);
        userService.export(request, response, userPageSearchBo);
    }

    @PutMapping("/batch-enable")
    @ApiOperation("批量启用")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> batchEnable(@RequestBody List<String> uuids) {
        userService.batchEnable(uuids);
        return success();
    }

    @PutMapping("/batch-disable")
    @ApiOperation("批量禁用")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> batchDisable(@RequestBody List<String> uuids) {
        userService.batchDisable(uuids);
        return success();
    }

    @PutMapping("/batch-delete")
    @ApiOperation("批量删除")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> batchDelete(@RequestBody List<String> uuids) {
        userService.batchDelete(uuids);
        return success();
    }

    @GetMapping("/detail/{uuid}")
    @ApiOperation("用户详情")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<UserInfoVo> detail(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "用户uuid", required = true) String uuid) {
        return success(userService.detail(uuid));
    }

    @PutMapping("/update/{uuid}")
    @ApiOperation("修改用户")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> updateUser(
        @RequestBody @Valid @ApiParam(value = "用户修改Ro", required = true)
        UserUpdateRo userUpdateRo,
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "用户uuid", required = true) String uuid) {
        UserUpdateBo userUpdateBo = BeanUtil.copyProperties(userUpdateRo, UserUpdateBo.class);
        userUpdateBo.setUserUuid(uuid);
        userService.updateUser(userUpdateBo);
        return success();
    }

    @PutMapping("/reset-password/{uuid}")
    @ApiOperation("重置密码")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> resetPassword(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "用户uuid", required = true) String uuid) {
        userService.resetPassword(uuid);
        return success();
    }
}
